package net.caixiaomi.info.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.MatchInfoForTeam;
import net.caixiaomi.info.model.MatchTeamFutureInfo;
import net.caixiaomi.info.model.MatchTeamInfo;
import net.caixiaomi.info.model.TeamScoreInfoItem;
import net.caixiaomi.info.model.TeamScoreInfoModel;
import net.caixiaomi.info.util.Constants;

import java.math.BigDecimal;
import java.util.List;

public class MatchDetailAnalysisAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private String mHostName;
    private String mGuestName;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MatchDetailAnalysisAdapter(List<MultiItemEntity> data) {
        super(data);

        addItemType(Constants.ITEM_VIEW_HEADER, R.layout.header_match_detail_analysis);
        addItemType(Constants.ITEM_VIEW_HEADER1, R.layout.header_analisis_histroy_battle);
        addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_match_detail_analysis);
        addItemType(Constants.ITEM_VIEW_FOOTER, R.layout.footer_analisis_score);
        addItemType(Constants.ITEM_VIEW_FOOTER1, R.layout.item_match_detail_future_analysis);
        addItemType(Constants.ITEM_VIEW_FOOTER2, R.layout.footer_analisis_future_battle);
    }

    public void setHostAndGuest(String hostName, String guestName) {
        mHostName = hostName;
        mGuestName = guestName;
    }


    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        try {
            switch (item.getItemType()) {
                case Constants.ITEM_VIEW_HEADER:
                    bindHeader(helper, item);
                    break;
                case Constants.ITEM_VIEW_ITEM:
                    bindItem(helper, item);
                    break;
                case Constants.ITEM_VIEW_HEADER1:
                    bindHistoryBattle(helper, item);
                    break;
                case Constants.ITEM_VIEW_FOOTER:
                    bindScore(helper, item);
                    break;

                case Constants.ITEM_VIEW_FOOTER1:
                    bindFutureBattle(helper, item);
                    break;
                case Constants.ITEM_VIEW_FOOTER2:
                    bindFutureHeader(helper, item);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindFutureHeader(BaseViewHolder helper, MultiItemEntity item){
        try {
            MatchTeamFutureInfo model = (MatchTeamFutureInfo) item;
            helper.setText(R.id.title, model.isHost()?mHostName:mGuestName);
            helper.setGone(R.id.anaylist_furture_group,!model.isNoData());
            helper.setGone(R.id.lineup_text_empty,model.isNoData());
        }catch (Exception e){
        }
    }

    private void bindFutureBattle(BaseViewHolder helper, MultiItemEntity item) {
        try {
            MatchTeamFutureInfo model = (MatchTeamFutureInfo) item;
            helper.setText(R.id.league, model.getLeagueAbbr());
            helper.setText(R.id.time, model.getMatchDate());
            helper.setText(R.id.host, model.getHomeTeamAbbr());
            helper.setText(R.id.guest, model.getVisitorTeamAbbr());
            TextView host=helper.getView(R.id.host);
            TextView guest=helper.getView(R.id.guest);
            if(model.isHost()) {
                if (host.getText().equals(mHostName)) {
                    host.setTextColor(mContext.getResources().getColor(R.color.primary_text));
                    guest.setTextColor(mContext.getResources().getColor(R.color.third_text));
                } else {
                    host.setTextColor(mContext.getResources().getColor(R.color.third_text));
                    guest.setTextColor(mContext.getResources().getColor(R.color.primary_text));
                }
            }else{
                if (host.getText().equals(mGuestName)) {
                    host.setTextColor(mContext.getResources().getColor(R.color.primary_text));
                    guest.setTextColor(mContext.getResources().getColor(R.color.third_text));
                } else {
                    host.setTextColor(mContext.getResources().getColor(R.color.third_text));
                    guest.setTextColor(mContext.getResources().getColor(R.color.primary_text));
                }
            }
        }catch (Exception e){

        }
    }

    private void bindScore(BaseViewHolder helper, MultiItemEntity item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            TeamScoreInfoModel model = (TeamScoreInfoModel) item;
            helper.setGone(R.id.title, model.isHost());
            TeamScoreInfoItem tteamScore = model.getTteamScore();//总
            TeamScoreInfoItem lteamScore = model.getLteamScore();//客
            TeamScoreInfoItem hteamScore = model.getHteamScore();//主
            if(tteamScore==null){
                tteamScore=new TeamScoreInfoItem();
            }
            if(lteamScore==null){
                lteamScore=new TeamScoreInfoItem();
            }
            if(hteamScore==null){
                hteamScore=new TeamScoreInfoItem();
            }

            helper.setText(R.id.name, model.getTeamName());

            stringBuilder.append(tteamScore.getMatchNum()).append("\n").append(hteamScore.getMatchNum()).append("\n").append(lteamScore.getMatchNum());

            helper.setTextColor(R.id.num, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.num, stringBuilder);
            stringBuilder.setLength(0);

            stringBuilder.append(tteamScore.getMatchH()).append("\n").append(hteamScore.getMatchH()).append("\n").append(lteamScore.getMatchH());
            helper.setTextColor(R.id.win, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.win, stringBuilder);
            stringBuilder.setLength(0);

            stringBuilder.append(tteamScore.getMatchD()).append("\n").append(hteamScore.getMatchD()).append("\n").append(lteamScore.getMatchD());
            helper.setTextColor(R.id.draw, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.draw, stringBuilder);
            stringBuilder.setLength(0);

            stringBuilder.append(tteamScore.getMatchL()).append("\n").append(hteamScore.getMatchL()).append("\n").append(lteamScore.getMatchL());
            helper.setTextColor(R.id.defeat, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.defeat, stringBuilder);
            stringBuilder.setLength(0);

            if(tteamScore.getBallIn().equals("-")){
                stringBuilder.append("---").append("\n").append("---").append("\n").append("---");
                helper.setTextColor(R.id.lose, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
                helper.setText(R.id.lose, stringBuilder);
                stringBuilder.setLength(0);
            }else{
                if(TextUtils.isEmpty(tteamScore.getBallClean())){
                    tteamScore.setBallClean("-");
                    hteamScore.setBallClean("-");
                    lteamScore.setBallClean("-");
                }
                stringBuilder.append(tteamScore.getBallIn()).append("/").append(tteamScore.getBallLose()).append("/").append(tteamScore.getBallClean()).append("\n").append(hteamScore.getBallIn()).append("/").append(hteamScore.getBallLose()).append("/").append(tteamScore.getBallClean()).append("\n").append(lteamScore.getBallIn()).append("/").append(lteamScore.getBallLose()).append("/").append(tteamScore.getBallClean());
                helper.setTextColor(R.id.lose, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
                helper.setText(R.id.lose, stringBuilder);
                stringBuilder.setLength(0);
            }

            stringBuilder.append(tteamScore.getScore()).append("\n").append(hteamScore.getScore()).append("\n").append(lteamScore.getScore());
            helper.setTextColor(R.id.score, ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
            helper.setText(R.id.score, stringBuilder);
            stringBuilder.setLength(0);

            stringBuilder.append(tteamScore.getTeamOrder()).append("\n").append(hteamScore.getTeamOrder()).append("\n").append(lteamScore.getTeamOrder());
            helper.setTextColor(R.id.order_num, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.order_num, stringBuilder);
            stringBuilder.setLength(0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindHistoryBattle(BaseViewHolder helper, MultiItemEntity item) {
        try {
            MatchTeamInfo model = (MatchTeamInfo) item;
            StringBuilder stringBuilder = new StringBuilder();
            String historyBattle = CommonApp.getInstance().getString(R.string.C_HISTORY_BATTLE);
            String matchNum = CommonApp.getInstance().getString(R.string.C_RECENTLY_MATCH_NUM, model.getTotal());

            stringBuilder.setLength(0);
            String hostWin = stringBuilder.append(model.getWin()).append(CommonApp.getInstance().getString(R.string.C_WIN)).toString();
            stringBuilder.setLength(0);
            String hostDraw = stringBuilder.append(model.getDraw()).append(CommonApp.getInstance().getString(R.string.C_DRAW)).toString();
            stringBuilder.setLength(0);
            String hostDefeat = stringBuilder.append(model.getLose()).append(CommonApp.getInstance().getString(R.string.C_DEFEAT)).toString();
            stringBuilder.setLength(0);

            stringBuilder.append(historyBattle).append("  ").append(matchNum).append("  ").append(CommonApp.getInstance().getString(R.string.C_HOST)).append("  ").append(hostWin).append("  ").append(hostDraw).append("  ").append(hostDefeat);
            SpannableString spannableString = new SpannableString(stringBuilder);
            int start = stringBuilder.indexOf(matchNum);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), start, start + matchNum.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            start = stringBuilder.indexOf(hostWin);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), start, start + hostWin.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            start = stringBuilder.indexOf(hostDraw);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.blue)), start, start + hostDraw.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            start = stringBuilder.indexOf(hostDefeat);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.green)), start, start + hostDefeat.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.text, spannableString);
            String toPlain = new BigDecimal(model.getWin()).divide(new BigDecimal(model.getTotal()), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
            double progress = Double.valueOf(toPlain) * 100;
            helper.setProgress(R.id.host, (int) progress, 100);
            toPlain = new BigDecimal(model.getDraw()).divide(new BigDecimal(model.getTotal()), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
            progress = Double.valueOf(toPlain) * 100;
            helper.setProgress(R.id.draw, (int) progress, 100);
            toPlain = new BigDecimal(model.getLose()).divide(new BigDecimal(model.getTotal()), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
            progress = Double.valueOf(toPlain) * 100;
            helper.setProgress(R.id.guest, (int) progress, 100);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindHeader(BaseViewHolder helper, MultiItemEntity item) {
        try {
            MatchTeamInfo model = (MatchTeamInfo) item;
            helper.setGone(R.id.text, !TextUtils.isEmpty(model.getTeamAbbr()));
            helper.setGone(R.id.action_divider, !TextUtils.isEmpty(model.getTeamAbbr()));
            helper.setGone(R.id.line, !TextUtils.isEmpty(model.getTeamAbbr()));
            helper.setGone(R.id.divider, TextUtils.isEmpty(model.getTeamAbbr()));
            if (!TextUtils.isEmpty(model.getTeamAbbr())) {
                StringBuilder stringBuilder = new StringBuilder();
                String matchNum = CommonApp.getInstance().getString(R.string.C_MATCH_NUM, model.getTotal());

                stringBuilder.setLength(0);
                String hostWin = stringBuilder.append(model.getWin()).append(CommonApp.getInstance().getString(R.string.C_WIN)).toString();
                stringBuilder.setLength(0);
                String hostDraw = stringBuilder.append(model.getDraw()).append(CommonApp.getInstance().getString(R.string.C_DRAW)).toString();
                stringBuilder.setLength(0);
                String hostDefeat = stringBuilder.append(model.getLose()).append(CommonApp.getInstance().getString(R.string.C_DEFEAT)).toString();
                stringBuilder.setLength(0);

                stringBuilder.append(CommonApp.getInstance().getString(R.string.C_RECENT_BATTLE)).append("  ").append(matchNum).append("  ").append(model.getTeamAbbr()).append("  ").append(hostWin).append("  ").append(hostDraw).append("  ").append(hostDefeat);
                SpannableString spannableString = new SpannableString(stringBuilder);
                int start = stringBuilder.indexOf(matchNum);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), start, start + matchNum.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                start = stringBuilder.indexOf(hostWin);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), start, start + hostWin.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                start = stringBuilder.indexOf(hostDraw);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.blue)), start, start + hostDraw.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                start = stringBuilder.indexOf(hostDefeat);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.green)), start, start + hostDefeat.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.text, spannableString);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindItem(BaseViewHolder helper, MultiItemEntity item) {
        try {
            int adapterPosition = helper.getAdapterPosition();
            MatchInfoForTeam model = (MatchInfoForTeam) item;

            helper.setText(R.id.league, model.getLeagueAddr());
            helper.setText(R.id.time, model.getMatchDay());
            helper.setText(R.id.host, model.getHomeTeamAbbr());
            helper.setText(R.id.score, model.getWhole());
            helper.setText(R.id.guest, model.getVisitingTeamAbbr());
            helper.setText(R.id.result, model.getMatchRs());
            int textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text);
            int hostColor = textColor;
            int guestColor = textColor;
            if (TextUtils.equals(model.getMatchRs(), CommonApp.getInstance().getResources().getString(R.string.C_WIN))) {
                textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary);
            } else if (TextUtils.equals(model.getMatchRs(), CommonApp.getInstance().getResources().getString(R.string.C_DRAW))) {
                textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.blue);
            } else if (TextUtils.equals(model.getMatchRs(), CommonApp.getInstance().getResources().getString(R.string.C_DEFEAT))) {
                textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.green);
            }
            helper.setTextColor(R.id.result, textColor);
            helper.setTextColor(R.id.score, textColor);
            if (model.getIndex() == 1) {//主队
                if (TextUtils.equals(model.getHomeTeamAbbr(), mHostName)) {
                    hostColor = textColor;
                }
                if (TextUtils.equals(model.getVisitingTeamAbbr(), mHostName)) {
                    guestColor = textColor;
                }
            } else if (model.getIndex() == 2) {
                if (TextUtils.equals(model.getHomeTeamAbbr(), mGuestName)) {
                    hostColor = textColor;
                }
                if (TextUtils.equals(model.getVisitingTeamAbbr(), mGuestName)) {
                    guestColor = textColor;
                }
            }
            helper.setTextColor(R.id.host, hostColor);
            helper.setTextColor(R.id.guest, guestColor);

            if (adapterPosition + 1 <= getData().size() - 1) {
                MultiItemEntity next = getData().get(adapterPosition + 1);
                helper.setGone(R.id.line, model.getItemType() == next.getItemType());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
